package com.zzp.zjs_bos.dao.base;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zzp.zjs_bos.utils.PageBean;

public class BaseDaoImpl<T> extends HibernateDaoSupport implements IBaseDao<T> {
	
	//给父类的sessionFactory提供工厂，在spring的配置文件里已经创建好了SessionFactory
	@Resource
	public void setSF(SessionFactory sessionFactory){
		//调用父类的方法注入会话工厂对象
		super.setSessionFactory(sessionFactory);
	}
	
	private Class<T> doMain;
	public BaseDaoImpl(){
		//获取父类
		ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();
		//获取父类上的泛型
		Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
		doMain = (Class<T>) actualTypeArguments[0];
	}

	public void save(T t) {
		this.getHibernateTemplate().save(t);
	}

	public void delete(T t) {
		this.getHibernateTemplate().delete(t);
	}

	public void update(T t) {
		this.getHibernateTemplate().update(t);
	}

	public List<T> findAll() {
		String hql = "from "+doMain.getSimpleName();
		return this.getHibernateTemplate().find(hql);
	}

	public T findById(Serializable id) {
		return this.getHibernateTemplate().get(doMain, id);
	}

	public List<T> findByCriteria(DetachedCriteria criteria) {
		return this.getHibernateTemplate().findByCriteria(criteria);
				
	}

	public List<T> findByNameQuery(String queryName, Object... args) {
		return this.getHibernateTemplate().findByNamedQuery(queryName, args);
	}
	
	public void executeNamedQuery(String queryName,Object...args){
		Session session = this.getSession();
		Query query = session.getNamedQuery(queryName);
		if(args != null && args.length >0){
			int i = 0;
			for (Object arg : args) {
				query.setParameter(i++, arg);
			}
		}
		query.executeUpdate();
	}
	
	
	//通用的分页方法
	public void pageQuery(PageBean pageBean){
		int currPage = pageBean.getCurrPage();
		int pageSize = pageBean.getPageSize();
		DetachedCriteria detacheCriteria = pageBean.getDetachedCriteria();
		
		//这里的查询语句是select count(*) from 表
		detacheCriteria.setProjection(Projections.rowCount());
		List<Long> list = this.getHibernateTemplate().findByCriteria(detacheCriteria);
		Long total =  0L;
		if(list.size() > 0 && list != null){
			total = list.get(0);
		}
		
		//这里要清空一次，不然查询语句还是select count(*) from 表，而不是想要的select * from 表
		detacheCriteria.setProjection(null);
		
		
		//这里是将查询出来的每一条记录都转换成对象的形式
		detacheCriteria.setResultTransformer(DetachedCriteria.ROOT_ENTITY);
		
		int firstResult = (currPage - 1) * pageSize;
		int maxResult = pageSize;
		
		List rows = this.getHibernateTemplate().findByCriteria(detacheCriteria, firstResult, maxResult);
		
		pageBean.setRows(rows);
		pageBean.setTotalCount(total.intValue());
	
	}
}
